package Command;

import java.util.LinkedList;
import Server.Command;

public class HistoryTracker {

	private LinkedList<Command> commandHistory; // for undo
	private LinkedList<Command> undoHistory; // for redo

	private HistoryTracker() {
		commandHistory = new LinkedList<Command>();
		undoHistory = new LinkedList<Command>();
	}

	private static final ThreadLocal<HistoryTracker> _localStorage = new ThreadLocal<HistoryTracker>() {
		protected HistoryTracker initialValue() {
			System.out.println("Creating HistoryTracker for Thread : " + Thread.currentThread().getName());

			return new HistoryTracker();
		}
	};

	public static HistoryTracker getInstance() {
		return _localStorage.get();
	}

	public void addCommand(Command cmd) {
		commandHistory.addFirst(cmd);
	}

	public void addUndo(Command cmd) {
		undoHistory.addFirst(cmd);
	}

	public Command pollLastUndo() {
		Command last = null;
		if (!undoHistory.isEmpty())
			last = undoHistory.removeFirst();
		return last;

	}

	public Command pollLastCommand() {
		Command last = null;
		if (!commandHistory.isEmpty())
			last = commandHistory.removeFirst();
		return last;
	}

}

